package cn.jn.lhm.part07;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author lhm 2753914576@qq.com
 * @time 2024/10/17 - 下午10:34
 **/
public class BuTask implements Runnable {

    private ByteBuffer byteBuffer;

    private final SocketChannel socketChannel;


    public BuTask(ByteBuffer byteBuffer, SocketChannel socketChannel) {
        this.byteBuffer = byteBuffer;
        this.socketChannel = socketChannel;
    }

    @Override
    public void run() {
        byteBuffer.flip();

        // log client message
        System.out.println("client message: " + new String(byteBuffer.array(), 0, byteBuffer.limit()));

        byteBuffer = null;

        // write data to client
        try {
            socketChannel.write(ByteBuffer.wrap("hello client".getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
